<template>
  <sky-base-page bg-page-color="#fff" padding="2rpx 0" footer-padding="12rpx 32rpx">
    <view class="recharge-page">

      <user-balance ref="userBalanceRef"></user-balance>

      <!-- 充值钻石选择 -->
      <view class="recharge-section">
        <view class="section-title">提现</view>
        <uni-forms ref="formRef" :model="form" :rules="rules" label-position="top"
                   err-show-type="toast" label-width="400rpx">
          <uni-forms-item name="money" label="钻石" required>
            <view class="input-box">
              <up-input type="digit" v-model="form.money" border="none"
                        clearable placeholder="请输入提现钻石"/>
            </view>
          </uni-forms-item>
        </uni-forms>
      </view>
    </view>
    <template #footer>
      <u-button type="primary" shape="circle" text="确 定" @tap="qly.form.pass(submit,formRef)"></u-button>
    </template>

  </sky-base-page>
</template>

<script setup>

const userBalanceRef=ref()

const formRef = ref()
const form = ref({
  money: '',
})

const rules = {
  // 对name字段进行必填验证
  money: {
    // name 字段的校验规则
    rules: [
      // 校验 name 不能为空
      {
        required: true,
        errorMessage: '请输入提现钻石',
      },
    ],
  },
}

const submit=async()=>{
  const res = await qly.api.user.doTransfer(form.value)
  if (res.code) {
    userBalanceRef.value.getUserInfo()
    checkJsApi(res.data)
  } else {
    qly.dialog.apiError(res).catch()
  }
}

const checkJsApi=(data)=>{
  console.log('data',data)
  WeixinJSBridge.invoke('requestMerchantTransfer', {
        mchId: data.mchId,
        appId: data.appId,
        package: data.package_info,
      },
      function (res) {
        if (res.err_msg === 'requestMerchantTransfer:ok') {
          // res.err_msg将在页面展示成功后返回应用时返回success，并不代表付款成功
          qly.dialog.success('提现成功')
        }
      }
  );
}
</script>

<style scoped lang="scss">
/* 余额卡片 */
.balance-card {
  background: #3c9cff;
  color: #fff;
  padding: 24px 16px;
  border-radius: 16px;
  margin: 16px;
  box-shadow: 0 8px 24px rgba(30, 136, 229, 0.2);
}

.balance-label {
  font-size: 14px;
  opacity: 0.9;
  margin-bottom: 8px;
}

.balance-amount {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 4px;
}

.recharge-tip {
  font-size: 12px;
  opacity: 0.8;
}

/* 充值区域 */
.recharge-section {
  background-color: #fff;
  margin: 16px;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03);
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 16px;
}

/* 钻石选择网格 */
.amount-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}

.amount-item {
  background-color: #F5F7FA;
  border-radius: 8px;
  padding: 16px 0;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
}

.amount-item.active {
  background-color: #E3F2FD;
  border: 2px solid #1E88E5;
  transform: translateY(-2px);
}

.amount-value {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.amount-gift {
  font-size: 11px;
  color: #FF9800;
  margin-top: 4px;
}

.discount-tip text {
  margin-left: 8px;
  line-height: 1.4;
}

.record-box {
  display: flex;
  justify-content: flex-end;
  padding: 10rpx 32rpx 0;
  color: #3C9CFF;
}


.input-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 80rpx;
  background: #F5F5F5;
  border-radius: 16rpx;
  font-size: 32rpx;
  box-sizing: border-box;
  padding: 0 24rpx;

  .input {
    width: 100%;
  }

  .code-box {
    margin-right: 50rpx;
  }
}
</style>
